package com.example.fitnessservice.api.web;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.example.fitnessservice.module.dao.UserLoginDao;
import com.example.fitnessservice.module.pojo.Result;
import com.example.fitnessservice.module.pojo.UserLogin;

import com.example.fitnessservice.module.utils.RandomUtils;
import com.example.fitnessservice.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/users")
public class UserLoginController {
    @Autowired
    private UserLoginService userLoginService;
//短信验证
@GetMapping("send/{phone}/interAspect")
public Result sendMsm(@PathVariable String phone) throws Exception {
    Result result = new Result();

    String code = RandomUtils.getFourBitRandom();
    Config config = new Config()
            //这里修改为我们上面生成自己的AccessKey ID
            .setAccessKeyId("LTAI5tGYrTdj892WCtYo6ZDA")

            //这里修改为我们上面生成自己的AccessKey Secret
            .setAccessKeySecret("zv1ZtG2Rnl113Ih3ZfJrVvJvemFpJX");
    // 访问的域名
    config.endpoint = "dysmsapi.aliyuncs.com";
    Client client = new Client(config);
    SendSmsRequest sendSmsRequest = new SendSmsRequest()
            .setSignName("阿里云短信测试")//短信签名
            .setTemplateCode("SMS_154950909")//短信模板
            .setPhoneNumbers(phone)//这里填写接受短信的手机号码
            .setTemplateParam("{'code':"+code+"}");//验证码


    // 复制代码运行请自行打印 API 的返回值
    SendSmsResponse sms = client.sendSms(sendSmsRequest);

    System.out.println(code);
    result.setMsg(code);
    return result;
}



    //校验用户名是否重复
    @GetMapping("/{username}/verify")
    public Result verify(@PathVariable String username) {
        UserLogin flag = userLoginService.username(username);
        Result result = new Result();
        if (flag==null) {
            //   成功
            result.setCode(200);
            result.setMsg("用户名可用");
        }else {
            result.setCode(500);
            result.setMsg("用户名已存在");
        }
        return result;
    }


    //注册
    @GetMapping("/{username}/{password}/{phoneNum}/register")
    public Result register(@PathVariable String username,@PathVariable String password,@PathVariable String phoneNum){
        UserLogin userLogin = new UserLogin();
        userLogin.setUsername(username);
        userLogin.setPassword(password);
        userLogin.setPhoneNum(phoneNum);
            Result result = new Result();
        System.out.println("register......."+userLogin);
            Boolean flag = userLoginService.register(userLogin);
            if (flag) {
//            成功
                result.setCode(200);
                result.setMsg("添加成功");
            }else {
                result.setCode(500);
                result.setMsg("添加失败");
            }
            return result;
        }



    //登录
    @GetMapping("/{username}/{password}/login")
    public Result login(@PathVariable String username, @PathVariable String password, HttpServletRequest request){

        Result result = new Result();
        UserLogin userLogin = new UserLogin();

        userLogin.setUsername(username);
        userLogin.setPassword(password);
        System.out.println("login......."+userLogin);
        UserLogin user = userLoginService.login(userLogin);
        if (user!=null) {
//            成功
            request.getSession().setAttribute("user",user);
            result.setCode(200);
            result.setMsg("登录成功");
            result.setData(user);
        }else {
            result.setCode(500);
            result.setMsg("用户名或者密码错误，登录失败");

        }
        return result;
    }


}
